<html><head>
      <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
   <title>JsonPath</title><link rel="stylesheet" type="text/css" href="eclipse_book.css"><meta name="generator" content="DocBook XSL Stylesheets V1.77.1"><link rel="home" href="index.html" title="Red Hat Fuse Tooling"><link rel="up" href="RiderExLang.html" title="Part&nbsp;III.&nbsp;Expression and Predicates Languages"><link rel="prev" href="JavaScript.html" title="JavaScript"><link rel="next" href="JXPath.html" title="JXPath"></head><body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF"><div class="chapter"><div class="titlepage"><div><div><h2 class="title"><a name="JsonPath"></a>JsonPath</h2></div></div></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_overview_48"></a>Overview</h2></div></div></div><p>The JsonPath language provides a convenient syntax for extracting portions of a JSON
message. The syntax of JSON is similar to XPath, but it is used to extract JSON objects
from a JSON message, instead of acting on XML. The <code class="literal">jsonpath</code> DSL command can
be used either as an expression or as a predicate (where an empty result gets
interpreted as boolean <code class="literal">false</code>).</p></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_adding_the_jsonpath_package"></a>Adding the JsonPath package</h2></div></div></div><p>To use JsonPath in your Camel routes, you need to add a dependency on
<code class="literal">camel-jsonpath</code> to your project, as follows:</p><pre class="screen">&lt;dependency&gt;
  &lt;groupId&gt;org.apache.camel&lt;/groupId&gt;
  &lt;artifactId&gt;camel-jsonpath&lt;/artifactId&gt;
  &lt;version&gt;${camel-version}&lt;/version&gt;
&lt;/dependency&gt;</pre></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_java_example"></a>Java example</h2></div></div></div><p>The following Java example shows how to use the <code class="literal">jsonpath()</code> DSL command to
select items in a certain price range:</p><pre class="screen">from("queue:books.new")
  .choice()
    .when().jsonpath("$.store.book[?(@.price &lt; 10)]")
      .to("jms:queue:book.cheap")
    .when().jsonpath("$.store.book[?(@.price &lt; 30)]")
      .to("jms:queue:book.average")
    .otherwise()
      .to("jms:queue:book.expensive")</pre><p>If the JsonPath query returns an empty set, the result is interpreted as
<code class="literal">false</code>. In this way, you can use a JsonPath query as a predicate.</p></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_xml_example"></a>XML example</h2></div></div></div><p>The following XML example shows how to use the <code class="literal">jsonpath</code> DSL element to
define predicates in a route:</p><pre class="screen">&lt;camelContext id="camel" xmlns="http://camel.apache.org/schema/spring"&gt;
  &lt;route&gt;
    &lt;from uri="direct:start"/&gt;
    &lt;choice&gt;
      &lt;when&gt;
        &lt;jsonpath&gt;$.store.book[?(@.price &lt; 10)]&lt;/jsonpath&gt;
        &lt;to uri="mock:cheap"/&gt;
      &lt;/when&gt;
      &lt;when&gt;
        &lt;jsonpath&gt;$.store.book[?(@.price &lt; 30)]&lt;/jsonpath&gt;
        &lt;to uri="mock:average"/&gt;
      &lt;/when&gt;
      &lt;otherwise&gt;
        &lt;to uri="mock:expensive"/&gt;
      &lt;/otherwise&gt;
    &lt;/choice&gt;
  &lt;/route&gt;
&lt;/camelContext&gt;</pre></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_easy_syntax"></a>Easy Syntax</h2></div></div></div><p>When you wish to define a basic predicate using <code class="literal">jsonpath</code> syntax it can be a bit hard to remember the syntax. For example, to find out all the cheap books, you have to write the syntax as follows:</p><pre class="screen">$.store.book[?(@.price &lt; 20)]</pre><p>However, what if you could just write it as:</p><pre class="screen">store.book.price &lt; 20</pre><p>You can also omit the path if you just want to look at nodes with a price key:</p><pre class="screen">price &lt; 20</pre><p>To support this, there is a <code class="literal">EasyPredicateParser</code> which you use to define the predicate using a basic style. That means the predicate must not start with the <code class="literal">$</code> sign, and must include only one operator. The easy syntax is as follows:</p><pre class="screen">left OP right</pre><p>You can use Camel simple language in the right operator, for example,</p><pre class="screen">store.book.price &lt; ${header.limit}</pre></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_supported_message_body_types"></a>Supported Message Body Types</h2></div></div></div><p>Camel JSonPath supports message body using the following types:</p><div class="informaltable"><table border="1"><colgroup><col width="50%" class="col_1"><col width="50%" class="col_2"></colgroup><thead><tr><th align="left" valign="top">Type</th><th align="left" valign="top">Description</th></tr></thead><tbody><tr><td align="left" valign="top"><p>File</p></td><td align="left" valign="top"><p>Reading from files</p></td></tr><tr><td align="left" valign="top"><p>String</p></td><td align="left" valign="top"><p>Plain strings</p></td></tr><tr><td align="left" valign="top"><p>Map</p></td><td align="left" valign="top"><p>essage body as <code class="literal">java.util.Map</code> type</p></td></tr><tr><td align="left" valign="top"><p>List</p></td><td align="left" valign="top"><p>Message body as java.util.List type</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">POJO</code></p></td><td align="left" valign="top"><p>Optional If Jackson is on the classpath, then <code class="literal">camel-jsonpath</code> is able to use Jackson to read the message body as <code class="literal">POJO</code> and convert to <code class="literal">java.util.Map</code> which is supported by JSonPath. For example you can add <code class="literal">camel-jackson</code> as dependency to include Jackson.</p></td></tr><tr><td align="left" valign="top"><p><code class="literal">InputStream</code></p></td><td align="left" valign="top"><p>If none of the above types matches, then Camel will attempt to read the message body as an <code class="literal">java.io.InputStream</code>.</p></td></tr></tbody></table></div><p>If a message body is of unsupported type then an exception is thrown by default, however you can configure JSonPath to suppress exceptions.</p></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_suppress_exceptions"></a>Suppress Exceptions</h2></div></div></div><p>JsonPath will throw an exception if the path configured by the <code class="literal">jsonpath</code> expression is not found. The exception can be ignored by setting the <code class="literal">SuppressExceptions</code> option to true. For example, in the code below, adding the true option as part of the <code class="literal">jsonpath</code> parameters:</p><pre class="screen">from("direct:start")
    .choice()
        // use true to suppress exceptions
        .when().jsonpath("person.middlename", <code class="literal">true</code>)
            .to("mock:middle")
        .otherwise()
            .to("mock:other");</pre><p>In XML DSL use the following syntax:</p><pre class="screen">&lt;route&gt;
  &lt;from uri="direct:start"/&gt;
  &lt;choice&gt;
    &lt;when&gt;
      &lt;jsonpath suppressExceptions="true"&gt;person.middlename&lt;/jsonpath&gt;
      &lt;to uri="mock:middle"/&gt;
    &lt;/when&gt;
    &lt;otherwise&gt;
      &lt;to uri="mock:other"/&gt;
    &lt;/otherwise&gt;
  &lt;/choice&gt;
&lt;/route&gt;</pre></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_jsonpath_injection"></a>JsonPath injection</h2></div></div></div><p>When using bean integration to invoke a bean method, you can use JsonPath to extract a
value from the message and bind it to a method parameter. For example:</p><pre class="screen">// Java
public class Foo {

    @Consume(uri = "activemq:queue:books.new")
    public void doSomething(@JsonPath("$.store.book[*].author") String author, @Body String json) {
      // process the inbound message here
    }
}</pre></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_inline_simple_expressions"></a>Inline Simple Expressions</h2></div></div></div><p>New in Camel 2.18.</p><p>Camel supports inline <code class="literal">Simple</code> expressions in the <code class="literal">JsonPath</code> expressions. The <code class="literal">Simple</code> language insertions must be expressed in <code class="literal">Simple</code> syntax as shown below:</p><pre class="screen">from("direct:start")
  .choice()
    .when().jsonpath("$.store.book[?(@.price &lt; `${header.cheap}`)]")
      .to("mock:cheap")
    .when().jsonpath("$.store.book[?(@.price &lt; `${header.average}`)]")
      .to("mock:average")
    .otherwise()
      .to("mock:expensive");</pre><p>Turn off support for <code class="literal">Simple</code> expressions by setting the option <code class="literal">allowSimple=false</code> as shown below.</p><p>Java:</p><pre class="programlisting">// Java DSL
.when().jsonpath("$.store.book[?(@.price &lt; 10)]", `false, false`)</pre><p>XML DSL:</p><pre class="programlisting">// XML DSL
&lt;jsonpath allowSimple="false"&gt;$.store.book[?(@.price &amp;lt; 10)]&lt;/jsonpath&gt;</pre></div><div class="simplesect"><div class="titlepage"><div><div><h2 class="title" style="clear: both"><a name="_reference"></a>Reference</h2></div></div></div><p>For more details about JsonPath, see the <a class="link" href="https://code.google.com/p/json-path/" target="_top">JSonPath project page</a>.</p></div></div></body></html>